package com.ict.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.core.domain.entity.SysDept;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.system.mapper.SysUserRoleMapper;
import com.ict.system.service.ISysDeptService;
import com.ict.web.constant.Constants;
import com.ict.web.constant.ProjectStatusEnum;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.*;
import com.ict.web.dto.ComplianceAddDto;
import com.ict.web.dto.ComplianceEditDto;
import com.ict.web.mapper.ComplianceMapper;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.util.CreateMsgUtil;
import com.ict.web.service.IApprovalService;
import com.ict.web.service.IComplianceService;
import com.ict.web.service.IProjectManagementService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.vo.ComplianceDetalisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 合规审查Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Service
public class ComplianceServiceImpl extends ServiceImpl<ComplianceMapper, Compliance> implements IComplianceService
{
    @Resource
    private ComplianceMapper complianceMapper;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Autowired
    private IYzqFileService fileService;
    @Resource
    private SysUserRoleMapper userRoleMapper;
    /**
     * 查询合规审查
     *
     * @param complianceId 合规审查主键
     * @return 合规审查
     */
    @Override
    public ComplianceDetalisVo selectComplianceByComplianceId(Long complianceId)
    {
        Compliance bean = complianceMapper.selectById(complianceId);
        ComplianceDetalisVo complianceDetalisVo = new ComplianceDetalisVo();
        BeanUtils.copyProperties(bean,complianceDetalisVo);
        YzqFile file = fileService.getById(bean.getTryEffectDescriptionStuff());
        if(StringUtils.isNotNull(file)){
            complianceDetalisVo.setTryEffectDescriptionStuffFile(file);
        }
        return complianceDetalisVo;
    }

    /**
     * 查询合规审查列表
     *
     * @param compliance 合规审查
     * @return 合规审查
     */
    @Override
    public List<Compliance> selectComplianceList(Compliance compliance)
    {
        return complianceMapper.selectComplianceList(compliance);
    }

    /**
     * 新增合规审查
     *
     * @param dto 合规审查
     * @return 结果
     */
    @Override
    public Long insertCompliance(ComplianceAddDto dto)
    {
        Compliance bean = ComplianceAddDto.of(dto);

        //新增里程碑信息-生成对应系统消息
        SysDept sysDept = deptService.selectDeptById(SecurityUtils.getDeptId());
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        org.springframework.util.Assert.isTrue(StringUtils.isNotNull(projectManagement),"未找到该项目信息");
        //大数据局收到消息
        List<Long> bigDataId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA);
        List<Long> bigDataNormalId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA_NORMAL);
        bigDataId.addAll(bigDataNormalId);
        bigDataId.forEach(s->{
            String content=projectManagement.getNickName()+ CreateMsgUtil.SUBMITTED+projectManagement.getName()+CreateMsgUtil.COMPLIANCE_MANAGEMENT_SUBMITTED;
            CreateMsgUtil.createMsg(content,s,null,CreateMsgUtil.COMPLIANCE_MANAGEMENT_SUBMITTED_TITLE, SysMsg.RESOURCE_TYPE_DATA,null, null);
        });
        //同步到项目信息
        projectManagement.setComplianceId(bean.getComplianceId());

        //存储合同扫码件文件
        YzqFile contractFile = dto.getTryEffectDescriptionStuff();
        contractFile.setId(String.valueOf(IdUtils.snowId()));
        contractFile.setDelFlag(ExtractExpert.DELETE_STATUS_NORMAL);
        fileService.save(contractFile);
        bean.setTryEffectDescriptionStuff(contractFile.getId());

        ReviewTask reviewTask = reviewTaskService.getById(projectManagement.getReviewTaskId());
        Assert.isTrue(StringUtils.isNotNull(reviewTask),"评审任务不能为空");
        Approval approval = approvalService.getApproval(projectManagement.getProId(), projectManagement.getIsState(),null,null);
        approval.setApprovalNode(ProjectStatusEnum.COMPLIANCE_REVIEW_SUBMITTED.get().getNode());
        approval.setApprovalStage(ProjectStatusEnum.COMPLIANCE_REVIEW_SUBMITTED.get().getStage());
        approval.setApprovalStatus(ProjectStatusEnum.COMPLIANCE_REVIEW_SUBMITTED.get().getStatus());
        approval.setEndTime(LocalDateTime.now());
        approval.setApprovalOperator(SecurityUtils.getNickName());
        approval.setApprovalDatabase(contractFile.getId());
        projectManagement.setIsState(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus());
        projectManagement.setNode(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode());
        projectManagement.setStag(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStage());
        //新增合规审查待审核
        approvalService.save(new Approval(IdUtils.snowId(),projectManagement.getProId(),ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStage(),
                ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode(), LocalDateTime.now(),null, null,ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus(),
                "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        reviewTask.setNode(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode());
        reviewTask.setNodeStatus(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus());
        projectManagementService.updateById(projectManagement);
        reviewTaskService.updateById(reviewTask);
        approvalService.updateById(approval);
        complianceMapper.insert(bean);
        return bean.getComplianceId();
    }

    /**
     * 修改合规审查
     *
     * @param dto 合规审查
     * @return 结果
     */
    @Override
    public int updateCompliance(ComplianceEditDto dto,Long complianceId)
    {
        Compliance bean = complianceMapper.selectById(complianceId);
        BeanUtils.copyProperties(dto, bean);
        bean.setUpdateTime(LocalDateTime.now());
        //同步合格审查文件信息
        YzqFile contractFile=null;
        if(StringUtils.isEmpty(dto.getTryEffectDescriptionStuff().getFileName())){
            if(StringUtils.isNotEmpty(bean.getTryEffectDescriptionStuff())){
                bean.setTryEffectDescriptionStuff(null);
            }
        }else {
            YzqFile file = fileService.getById(bean.getTryEffectDescriptionStuff());
            contractFile = dto.getTryEffectDescriptionStuff();
            if(StringUtils.isNotNull(file)) {
                if (!file.getFileName().equals(contractFile.getFileName()) &&
                        !file.getFileUrl().equals(contractFile.getFileUrl())) {
                    BeanUtils.copyProperties(contractFile,file);
                    fileService.updateById(file);
                    bean.setTryEffectDescriptionStuff(file.getId());
                }
            }else {
                contractFile.setId(String.valueOf(IdUtils.snowId()));
                fileService.save(contractFile);
                bean.setTryEffectDescriptionStuff(contractFile.getId());
            }
        }
        //通过评审任务、项目和审核进程状态
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        Assert.isTrue(StringUtils.isNotNull(projectManagement),"未找到该项目信息");
        ReviewTask reviewTask = reviewTaskService.getById(projectManagement.getReviewTaskId());
        Assert.isTrue(StringUtils.isNotNull(reviewTask),"评审任务不能为空");
        Approval approval = approvalService.getApproval(projectManagement.getProId(), projectManagement.getIsState(),null,null);
        approval.setEndTime(LocalDateTime.now());
        approvalService.updateById(approval);
        projectManagement.setIsState(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus());
        projectManagement.setNode(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode());
        projectManagement.setStag(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStage());
        reviewTask.setNode(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode());
        reviewTask.setNodeStatus(ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus());
        projectManagementService.updateById(projectManagement);
        reviewTaskService.updateById(reviewTask);
        approvalService.save(new Approval(IdUtils.snowId(),projectManagement.getProId(),ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStage(),
                ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getNode(),LocalDateTime.now(),null,null,ProjectStatusEnum.COMPLIANCE_REVIEW_TO_BE_REVIEWED.get().getStatus(),
                "/",null, null,contractFile!=null?contractFile.getId():null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        return complianceMapper.updateById(bean);
    }

//    /**
//     * 批量删除合规审查
//     *
//     * @param complianceIds 需要删除的合规审查主键
//     * @return 结果
//     */
//    @Override
//    public int deleteComplianceByComplianceIds(Long[] complianceIds)
//    {
//        return complianceMapper.deleteComplianceByComplianceIds(complianceIds);
//    }

    /**
     * 删除合规审查信息
     *
     * @param complianceId 合规审查主键
     * @return 结果
     */
    @Override
    public int deleteComplianceByComplianceId(Long complianceId)
    {
        Compliance bean = complianceMapper.selectById(complianceId);
        Assert.isTrue(bean != null, "数据不存在！");
        bean.setIsDelete("1");
        return complianceMapper.updateById(bean);
    }
}
